package com.zhongke.cotrun.controller;

import com.google.common.base.Preconditions;
import com.sun.istack.NotNull;
import com.zhongke.cotrun.common.annotation.SysLog;
import com.zhongke.cotrun.common.util.APIResponse;
import com.zhongke.cotrun.model.exception.BizException;
import com.zhongke.cotrun.model.pojo.PageQueryResult;
import com.zhongke.cotrun.model.pojo.ProjectInfo;
import com.zhongke.cotrun.model.pojo.ProjectInfoVo;
import com.zhongke.cotrun.service.ProjectInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 作者 yhl
 * 日期 2020-01-05 02:02:19
 * 描述 ProjectInfo 服务
 */

@RestController
@RequestMapping("/projectInfo")
@CrossOrigin
@Validated
@Api(tags = "项目信息服务")
public class ProjectInfoController extends AbstractController {


    @Resource
    public ProjectInfoService projectInfoService;

    @ApiOperation(value = "根据id查询审核通过后数据", notes = "根据id查询审核通过后数据:外网展示", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping(value = "/queryById.json")
    public APIResponse<ProjectInfo> queryById(@NotNull @ApiParam(value = "对象id", required = true) Integer id) {
        Preconditions.checkNotNull(id);
        Preconditions.checkArgument(id > 0, "id(%s)必须大于0！", id);
        return APIResponse.returnSuccess(projectInfoService.selectByPrimaryKey(id));
    }

    @ApiOperation(value = "根据id查询未审核数据", notes = "根据id查询未审核数据:审核详情页使用", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping(value = "/queryAuditDetailById.json")
    @ResponseBody
    @RequiresPermissions("web:project:info")
    public APIResponse<ProjectInfo> queryAuditDetailById(@NotNull @ApiParam(value = "对象id", required = true) Integer id) {
        Preconditions.checkNotNull(id);
        Preconditions.checkArgument(id > 0, "id(%s)必须大于0！", id);
        return APIResponse.returnSuccess(projectInfoService.selectAuditDetailByPrimaryKey(id));
    }

    @ApiOperation(value = "保存或更新未审核的项目数据", notes = "保存或更新未审核的项目数据，进审核表，保存或修改项目信息时使用", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/saveOrUpdateHistory.json")
    @ResponseBody
    @RequiresPermissions("web:project:saveOrUpdate")
    @SysLog(value="新增或修改项目",type="项目技术")
    public APIResponse<Boolean> saveOrUpdateHistory(
            @ApiParam(value = "未审核的项目数据", required = true) @Validated ProjectInfo record) throws BizException {
        Preconditions.checkNotNull(record);
        record.setApprovalState(0);
        record.setCreateId(getWebUserId());
        if(projectInfoService.insertSelectiveForHistory(record) > 0){
            return APIResponse.returnSuccess(true);
        }else{
            return APIResponse.returnFail("操作失败");
        }
    }

    @ApiOperation(value = "分页查询项目列表", notes = "分页查询项目列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping(value = "/pageQueryList.json",method = RequestMethod.POST)
    @ResponseBody
    public APIResponse<PageQueryResult<ProjectInfoVo>> pageQueryList(@ApiParam(value = "分页请求参数", required = true) @Validated ProjectInfoVo param) {
        Preconditions.checkNotNull(param);
        Preconditions.checkArgument(param.getPageIndex() > 0 && param.getPageSize() > 0);
        param.checkQueryParam();
        param.setState(0);
        param.setApprovalState(1);
        return APIResponse.returnSuccess(projectInfoService.pageQueryProjectList(param));
    }

    @ApiOperation(value = "会员中心分页查询项目列表", notes = "会员中心分页查询项目列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping(value = "/pageQueryListForCenter.json",method = RequestMethod.POST)
    @RequiresPermissions("web:project:list")
    public APIResponse<PageQueryResult<ProjectInfoVo>> pageQueryListForCenter(@ApiParam(value = "分页请求参数", required = true) @Validated ProjectInfoVo param) {
        Preconditions.checkNotNull(param);
        param.setCreateId(getWebUserId());
        Preconditions.checkArgument(param.getPageIndex() > 0 && param.getPageSize() > 0);
        param.checkQueryParam();
        return APIResponse.returnSuccess(projectInfoService.pageQueryProjectList(param));
    }

    @ApiOperation(value = "获取最新12条推荐项目信息", notes = "获取最新12条推荐项目信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping(value = "/listLast12Info.json", method = RequestMethod.POST)
    @ResponseBody
    public APIResponse<List<ProjectInfoVo>> listLast12Info() {
        return APIResponse.returnSuccess(projectInfoService.listLast12Info());
    }

    /**
     * 根据id删除对象
     */
    @ResponseBody
    @ApiOperation(value = "删除", notes = "删除", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping(value = "/deleteById.json")
    @RequiresPermissions("web:project:delete")
    @SysLog(value="删除项目",type="项目技术")
    public APIResponse<Boolean> deleteById(@ApiParam(value = "对象id", required = true) Integer id) {
        Preconditions.checkNotNull(id);
        Preconditions.checkArgument(id > 0, "id(%s)必须大于0！", id);
        if(projectInfoService.deleteByPrimaryKey(id) > 0){
            return APIResponse.returnSuccess(true);
        }else{
            return APIResponse.returnFail("操作失败");
        }
    }

    /**
     * 根据id下线项目
     */
    @ResponseBody
    @ApiOperation(value = "下线", notes = "下线", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/cancelById.json")
    @RequiresPermissions("web:project:cancel")
    @SysLog(value="下线项目",type="项目技术")
    public APIResponse<Boolean> cancelById(@ApiParam(value = "对象id", required = true) Integer id) {
        Preconditions.checkNotNull(id);
        Preconditions.checkArgument(id > 0, "id(%s)必须大于0！", id);
        if(projectInfoService.cancelByPrimaryKey(id)){
            return APIResponse.returnSuccess(true);
        }else{
            return APIResponse.returnFail("操作失败");
        }
    }

}
